home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / wysiwyg.arc / DEMO.C next >
C/C++ Source or Header  |  1991-04-28  |  19KB  |  599 lines

  1. /*--- DEMO.C ----------------------------- Listing 1 ------------
  2.  * Lists available fonts under Windows and shows approxi-
  3.  * mately what the printer versions look like. Requires SDK.
  4.  * Compiles under MSC 5.0-6.00A
  5.  *
  6.  * by Michael Young. Object code may be used freely. Source 
  7.  * code may be used freely if authorship and publication are
  8.  * caknowledged.
  9.  *-------------------------------------------------------------*/
  10. #define _WINDOWS
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #define IDC_BOLD         100
  16. #define IDC_FONTLIST     110
  17. #define IDC_ITALIC       120
  18. #define IDC_PRNNAME      130
  19. #define IDC_SIZELIST     140
  20. #define IDC_UNDERLINE    150
  21.  
  22. HWND HBold;
  23. HFONT HFont = NULL;
  24. HWND HFontList;
  25. HANDLE HInst;
  26. HWND HItalic;
  27. HDC HPrnIC = NULL;
  28. HWND HSizeList;
  29. HWND HUnderline;
  30. HDC HWinDC = NULL;
  31. HWND HWindow;
  32. int FontSize;
  33. LOGFONT LF;
  34. int NumSizes;
  35. char PrinterName [32];
  36. RECT Rect;
  37. int SizeList [25];
  38. TEXTMETRIC TM;
  39. int VectorFont;
  40. static int VectorSizes [] = { 6*20,  8*20, 10*20, 12*20, 14*20,
  41.                              18*20, 24*20, 30*20, 36*20, 48*20};
  42.  
  43. int FAR PASCAL EnumFaceNames (LPLOGFONT PtrLF,
  44.                               LPTEXTMETRIC PtrTM,
  45.                               short FontType,
  46.                               LPSTR PtrData);
  47. int FAR PASCAL EnumSizes     (LPLOGFONT PtrLF,
  48.                               LPTEXTMETRIC PtrTM,
  49.                               short FontType,
  50.                               LPSTR PtrData);
  51. void GetPrnIC (void);
  52. BOOL InitInstance (HANDLE hInstance, int nCmdShow);
  53. BOOL InitProgram (HANDLE hInstance);
  54. long FAR PASCAL MainWndProc (HWND hWnd,unsigned message,
  55.                              WORD wParam,LONG lParam);
  56.  
  57. int PASCAL WinMain
  58.      (HANDLE hInstance,
  59.      HANDLE hPrevInstance,
  60.      LPSTR lpCmdLine,
  61.      int nCmdShow)
  62.      {
  63.      MSG Msg;
  64.  
  65.      if (!hPrevInstance)
  66.           if (!InitProgram (hInstance))
  67.                return (FALSE);
  68.      if (!InitInstance (hInstance, nCmdShow))
  69.           return (FALSE);
  70.      while (GetMessage
  71.           (&Msg,
  72.           NULL,
  73.           NULL,
  74.           NULL))
  75.           {
  76.           TranslateMessage (&Msg);
  77.           DispatchMessage (&Msg);
  78.           }
  79.      return (Msg.wParam);
  80.      } /* end WinMain */
  81.  
  82. int FAR PASCAL EnumFaceNames (LPLOGFONT PtrLF,LPTEXTMETRIC PtrTM,
  83.                               short FontType,LPSTR PtrData)
  84.      {
  85.      SendMessage
  86.           (HFontList,
  87.           LB_ADDSTRING,
  88.           NULL,
  89.           (DWORD)(LPSTR)PtrLF->lfFaceName);
  90.      return (1);
  91.      } /* end EnumFaceNames */
  92.  
  93. int FAR PASCAL EnumSizes (LPLOGFONT PtrLF,LPTEXTMETRIC PtrTM,
  94.                             short FontType,LPSTR PtrData)
  95.      {
  96.      int i;
  97.      int PointSize;
  98.  
  99.      LF = *PtrLF;
  100.      if (!(FontType & RASTER_FONTTYPE))
  101.           {
  102.           VectorFont = 1;
  103.           return (0);
  104.           }
  105.      if (NumSizes >= 25)
  106.           return (0);
  107.      PointSize = (PtrTM->tmHeight -
  108.                   PtrTM->tmInternalLeading + 10) / 20;
  109.      PointSize = PointSize * 20 + 9;
  110.      for (i = 0; i < NumSizes; ++i)
  111.           {
  112.           if (SizeList [i] == PointSize)
  113.                return (1);
  114.           if (SizeList [i] > PointSize)
  115.                {
  116.                memmove (SizeList + i + 1,SizeList + i,
  117.                         (NumSizes - i) * sizeof (int));
  118.                break;
  119.                }
  120.           }
  121.      SizeList [i] = PointSize;
  122.      ++NumSizes;
  123.      return (1);
  124.      } /* end EnumSizes */
  125.  
  126. void GetPrnIC (void)
  127.      {
  128.      char PrnInfo [80];
  129.      char *PtrPrnDriver;
  130.      char *PtrPrnName;
  131.      char *PtrPrnPort;
  132.      int Result;
  133.  
  134.      Result = GetProfileString
  135.           ("windows",
  136.           "device",
  137.           (LPSTR)"",
  138.           PrnInfo,
  139.           sizeof (PrnInfo));
  140.      if (Result == 0 || !PrnInfo [0])
  141.           return;
  142.      PtrPrnName = strtok (PrnInfo,",");
  143.      PtrPrnDriver = strtok (NULL,", ");
  144.      PtrPrnPort = strtok (NULL,", ");
  145.      if (!PtrPrnName || !PtrPrnDriver || !PtrPrnPort)
  146.           return;
  147.      strcpy (PrinterName,PtrPrnName);
  148.      HPrnIC = CreateIC
  149.           (PtrPrnDriver,
  150.           PtrPrnName,
  151.           PtrPrnPort,
  152.           NULL);
  153.      if (HPrnIC == NULL)
  154.           return;
  155.      SetMapMode (HPrnIC,MM_ANISOTROPIC);
  156.      SetWindowExt (HPrnIC,1440,1440);
  157.      SetViewportExt
  158.           (HPrnIC,
  159.           GetDeviceCaps (HPrnIC,LOGPIXELSX),
  160.           GetDeviceCaps (HPrnIC,LOGPIXELSY));
  161.      return;
  162.      } /* end GetPrnIC */
  163.  
  164. BOOL InitInstance
  165.      (HANDLE hInstance,
  166.      int hCmdShow)
  167.      {
  168.      LONG DlgBaseUnits;
  169.      HDC HWinDC;
  170.      int Index;
  171.      FARPROC ProcAddr;
  172.  
  173.      HInst = hInstance;
  174.      DlgBaseUnits = GetDialogBaseUnits ();
  175.      Rect.left = (5 * LOWORD (DlgBaseUnits)) / 4;
  176.      Rect.top = (100 * HIWORD (DlgBaseUnits)) / 8;
  177.      Rect.right = (130 * LOWORD (DlgBaseUnits)) / 4;
  178.      Rect.bottom = (142 * HIWORD (DlgBaseUnits)) / 8;
  179.      HWindow = CreateWindow
  180.           ("DemoClass",            /* Class name passed
  181.                                       to RegisterClass. */
  182.           "Printer Font Viewer",   /* Text for window title. */
  183.           WS_BORDER      |         /* Window style: */
  184.           WS_CAPTION     |
  185.           WS_MINIMIZEBOX |
  186.           WS_SYSMENU     |
  187.           WS_VISIBLE,
  188.           CW_USEDEFAULT,      /* Horz. position: use default. */
  189.           CW_USEDEFAULT,      /* Vert. position: use default. */
  190.           (130 * LOWORD (DlgBaseUnits)) / 4, /* Window width. */
  191.           (142 * HIWORD (DlgBaseUnits)) / 8, /* Window height.*/
  192.           NULL,               /* Parent: none. */
  193.           NULL,               /* Menu: use class menu. */
  194.           HInst,              /* Instance to be associated
  195.                                  with window. */
  196.           NULL);              /* Pointer value passed
  197.                                  to window: none. */
  198.      if (!HWindow)
  199.           return (FALSE);
  200.      HWinDC = GetDC (HWindow);
  201.      SetMapMode
  202.           (HWinDC,
  203.           MM_ANISOTROPIC);
  204.      SetWindowExt (HWinDC,1440,1440);
  205.      SetViewportExt
  206.           (HWinDC,
  207.           GetDeviceCaps (HWinDC,LOGPIXELSX),
  208.           GetDeviceCaps (HWinDC,LOGPIXELSY));
  209.      SetViewportOrg
  210.           (HWinDC,
  211.           (5 * LOWORD (DlgBaseUnits)) / 4,
  212.           (100 * HIWORD (DlgBaseUnits)) / 8);
  213.      ReleaseDC (HWindow,HWinDC);
  214.      ShowWindow
  215.           (HWindow,
  216.           hCmdShow);
  217.      GetPrnIC ();
  218.      GetTextMetrics (HPrnIC,&TM);
  219.      FontSize = (TM.tmHeight -
  220.                     TM.tmInternalLeading + 10) / 20;
  221.      FontSize = FontSize * 20 + 9;
  222.      GetTextFace (HPrnIC,LF_FACESIZE,LF.lfFaceName);
  223.      CreateWindow
  224.           ("STATIC",
  225.           "Printer:",
  226.           WS_CHILD   |
  227.           WS_VISIBLE |
  228.           SS_LEFT,
  229.           (5 * LOWORD (DlgBaseUnits)) / 4,
  230.           (5 * HIWORD (DlgBaseUnits)) / 8,
  231.           (25 * LOWORD (DlgBaseUnits)) / 4,
  232.           (8 * HIWORD (DlgBaseUnits)) / 8,
  233.           HWindow,
  234.           -1,
  235.           HInst,
  236.           NULL);
  237.      CreateWindow
  238.           ("STATIC",
  239.           PrinterName,
  240.           WS_CHILD   |
  241.           WS_VISIBLE |
  242.           SS_LEFT,
  243.           (31 * LOWORD (DlgBaseUnits)) / 4,
  244.           (5 * HIWORD (DlgBaseUnits)) / 8,
  245.           (96 * LOWORD (DlgBaseUnits)) / 4,
  246.           (8 * HIWORD (DlgBaseUnits)) / 8,
  247.           HWindow,
  248.           IDC_PRNNAME,
  249.           HInst,
  250.           NULL);
  251.      CreateWindow
  252.           ("STATIC",
  253.           "Typeface:",
  254.           WS_CHILD   |
  255.           WS_VISIBLE |
  256.           SS_LEFT,
  257.           (5 * LOWORD (DlgBaseUnits)) / 4,
  258.           (17 * HIWORD (DlgBaseUnits)) / 8,
  259.           (35 * LOWORD (DlgBaseUnits)) / 4,
  260.           (8 * HIWORD (DlgBaseUnits)) / 8,
  261.           HWindow,
  262.           -1,
  263.           HInst,
  264.           NULL);
  265.      CreateWindow
  266.           ("STATIC",
  267.           "Size:",
  268.           WS_CHILD   |
  269.           WS_VISIBLE |
  270.           SS_LEFT,
  271.           (88 * LOWORD (DlgBaseUnits)) / 4,
  272.           (17 * HIWORD (DlgBaseUnits)) / 8,
  273.           (27 * LOWORD (DlgBaseUnits)) / 4,
  274.           (8 * HIWORD (DlgBaseUnits)) / 8,
  275.           HWindow,
  276.           -1,
  277.           HInst,
  278.           NULL);
  279.      HFontList = CreateWindow
  280.           ("LISTBOX",
  281.           "",
  282.           WS_CHILD |
  283.           WS_VSCROLL |
  284.           WS_VISIBLE |
  285.           LBS_STANDARD,
  286.           (5 * LOWORD (DlgBaseUnits)) / 4,
  287.           (28 * HIWORD (DlgBaseUnits)) / 8,
  288.           (76 * LOWORD (DlgBaseUnits)) / 4,
  289.           (57 * HIWORD (DlgBaseUnits)) / 8,
  290.           HWindow,
  291.           IDC_FONTLIST,
  292.           HInst,
  293.           NULL);
  294.      HSizeList = CreateWindow
  295.           ("LISTBOX",
  296.           "",
  297.           WS_CHILD   |
  298.           WS_VSCROLL |
  299.           WS_VISIBLE |
  300.           WS_BORDER  |
  301.           LBS_NOTIFY,
  302.           (88 * LOWORD (DlgBaseUnits)) / 4,
  303.           (28 * HIWORD (DlgBaseUnits)) / 8,
  304.           (36 * LOWORD (DlgBaseUnits)) / 4,
  305.           (57 * HIWORD (DlgBaseUnits)) / 8,
  306.           HWindow,
  307.           IDC_SIZELIST,
  308.           HInst,
  309.           NULL);
  310.      HBold = CreateWindow
  311.           ("BUTTON",
  312.           "Bold",
  313.           WS_CHILD   |
  314.           WS_VISIBLE |
  315.           BS_AUTOCHECKBOX,
  316.           (5 * LOWORD (DlgBaseUnits)) / 4,
  317.           (88 * HIWORD (DlgBaseUnits)) / 8,
  318.           (28 * LOWORD (DlgBaseUnits)) / 4,
  319.           (12 * HIWORD (DlgBaseUnits)) / 8,
  320.           HWindow,
  321.           IDC_BOLD,
  322.           HInst,
  323.           NULL);
  324.      SendMessage
  325.           (HBold,
  326.           BM_SETCHECK,
  327.           TM.tmWeight == 700,
  328.           NULL);
  329.      HItalic = CreateWindow
  330.           ("BUTTON",
  331.           "Italic",
  332.           WS_CHILD   |
  333.           WS_VISIBLE |
  334.           BS_AUTOCHECKBOX,
  335.           (43 * LOWORD (DlgBaseUnits)) / 4,
  336.           (88 * HIWORD (DlgBaseUnits)) / 8,
  337.           (28 * LOWORD (DlgBaseUnits)) / 4,
  338.           (12 * HIWORD (DlgBaseUnits)) / 8,
  339.           HWindow,
  340.           IDC_ITALIC,
  341.           HInst,
  342.           NULL);
  343.      SendMessage
  344.           (HItalic,
  345.           BM_SETCHECK,
  346.           TM.tmItalic,
  347.           NULL);
  348.      HUnderline = CreateWindow
  349.           ("BUTTON",
  350.           "Underline",
  351.           WS_CHILD   |
  352.           WS_VISIBLE |
  353.           BS_AUTOCHECKBOX,
  354.           (82 * LOWORD (DlgBaseUnits)) / 4,
  355.           (88 * HIWORD (DlgBaseUnits)) / 8,
  356.           (43 * LOWORD (DlgBaseUnits)) / 4,
  357.           (12 * HIWORD (DlgBaseUnits)) / 8,
  358.           HWindow,
  359.           IDC_UNDERLINE,
  360.           HInst,
  361.           NULL);
  362.      SendMessage
  363.           (HUnderline,
  364.           BM_SETCHECK,
  365.           TM.tmUnderlined,
  366.           NULL);
  367.      ProcAddr = MakeProcInstance (EnumFaceNames,HInst);
  368.      EnumFonts (HPrnIC,NULL,ProcAddr,NULL);
  369.      FreeProcInstance (ProcAddr);
  370.      Index = SendMessage
  371.           (HFontList,
  372.           LB_FINDSTRING,
  373.           -1,
  374.           (DWORD)(LPSTR)LF.lfFaceName);
  375.      SendMessage
  376.           (HFontList,
  377.           LB_SETCURSEL,
  378.           Index,
  379.           NULL);
  380.      SendMessage
  381.           (HWindow,
  382.           WM_COMMAND,
  383.           IDC_FONTLIST,
  384.           MAKELONG (0,LBN_SELCHANGE));
  385.      return (TRUE);
  386.      } /* end InitInstance */
  387.  
  388. BOOL InitProgram
  389.      (HANDLE hInstance)
  390.      {
  391.      WNDCLASS WC;
  392.  
  393.                   /* Style: private device context. */
  394.      WC.style = CS_OWNDC;
  395.                   /* Window function. */
  396.      WC.lpfnWndProc = MainWndProc;
  397.                   /* Extra data for class: none. */
  398.      WC.cbClsExtra = 0;
  399.                   /* Extra data for instance: none. */
  400.      WC.cbWndExtra = 0;
  401.                   /* Program instance that owns class */
  402.      WC.hInstance = hInstance;
  403.                   /* Load/assign class icon.*/
  404.      WC.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  405.                   /* Load/assign cl. cursor.*/
  406.      WC.hCursor = LoadCursor (NULL, IDC_ARROW);
  407.                   /* Background brush. */
  408.      WC.hbrBackground = GetStockObject (WHITE_BRUSH);
  409.                   /* Default class menu: none. */
  410.      WC.lpszMenuName = NULL;
  411.                   /* Name of window class. */
  412.      WC.lpszClassName = "DemoClass";
  413.                   /* Register window class/return code*/
  414.      return (RegisterClass (&WC));
  415.      } /* end InitProgram */
  416.  
  417. long FAR PASCAL MainWndProc
  418.      (HWND hWnd,
  419.      WORD wMsg,
  420.      WORD wParam,
  421.      LONG lParam)
  422. {
  423.     char Buffer [LF_FACESIZE];
  424.     int Checked;
  425.     HFONT HFontOld;
  426.     HDC HWinDC;
  427.     int i;
  428.     int IdxDefault;
  429.     int Index;
  430.     PAINTSTRUCT PaintStruct;
  431.     int PrevPoints;
  432.     FARPROC ProcAddr;
  433.  
  434.     switch (wMsg)
  435.     {
  436.     case WM_COMMAND:
  437.         switch (wParam)
  438.         {
  439.         case IDC_BOLD:
  440.             switch (HIWORD (lParam))
  441.             {
  442.             case BN_CLICKED:
  443.                 Checked = SendMessage (HBold,BM_GETCHECK,
  444.                                        NULL,NULL);
  445.                 LF.lfWeight = Checked ? 700 : 400;
  446.                 if (HFont != NULL)
  447.                     DeleteObject (HFont);
  448.                 HFont = CreateFontIndirect (&LF);
  449.                 InvalidateRect (HWindow,&Rect,TRUE);
  450.                 UpdateWindow (HWindow);
  451.                 return (NULL);
  452.             default:
  453.                 return (DefWindowProc (hWnd,wMsg,wParam,lParam));
  454.             }
  455.         case IDC_FONTLIST:
  456.             switch (HIWORD (lParam))
  457.             {
  458.             case LBN_SELCHANGE:
  459.                 Index = SendMessage
  460.                             (HFontList,
  461.                             LB_GETCURSEL,
  462.                             0,
  463.                             0L);
  464.                 SendMessage
  465.                     (HFontList,
  466.                     LB_GETTEXT,
  467.                     Index,
  468.                     (DWORD)(LPSTR)Buffer);
  469.                 SendMessage
  470.                     (HSizeList,
  471.                     LB_RESETCONTENT,
  472.                     0,
  473.                     0L);
  474.                 NumSizes = 0;
  475.                 VectorFont = 0;
  476.                 ProcAddr = MakeProcInstance (EnumSizes,HInst);
  477.                 EnumFonts (HPrnIC, Buffer, ProcAddr,NULL);
  478.                 FreeProcInstance (ProcAddr);
  479.                 LF.lfWidth = 0;
  480.                 Checked = SendMessage (HBold,
  481.                             BM_GETCHECK,NULL,NULL);
  482.                 LF.lfWeight = Checked ? 700 : 400;
  483.                 Checked = SendMessage (HItalic,
  484.                             BM_GETCHECK,NULL,NULL);
  485.                 LF.lfItalic = Checked;
  486.                 Checked = SendMessage (HUnderline,
  487.                             BM_GETCHECK,NULL,NULL);
  488.                 LF.lfUnderline = Checked;
  489.                 if (VectorFont)
  490.                 {
  491.                     memcpy (SizeList,VectorSizes,
  492.                                 sizeof (VectorSizes));
  493.                     NumSizes = sizeof (VectorSizes) /
  494.                                 sizeof (int);
  495.                 }
  496.                 PrevPoints = 0;
  497.                 IdxDefault = 0;
  498.                 for (i = 0; i < NumSizes; ++i)
  499.                 {
  500.                     sprintf (Buffer,"%d",SizeList [i] / 20);
  501.                     SendMessage
  502.                         (HSizeList,
  503.                         LB_ADDSTRING,
  504.                         NULL,
  505.                         (DWORD)(LPSTR)Buffer);
  506.                     if (SizeList [i] > PrevPoints &&
  507.                         SizeList [i] <= FontSize)
  508.                     {
  509.                         PrevPoints = SizeList [i];
  510.                         IdxDefault = i;
  511.                     }
  512.                 }
  513.                 SendMessage
  514.                     (HSizeList,
  515.                     LB_SETCURSEL,
  516.                     IdxDefault,
  517.                     NULL);
  518.                 SendMessage (HWindow,WM_COMMAND,IDC_SIZELIST,
  519.                              MAKELONG (0,LBN_SELCHANGE));
  520.                 return (NULL);
  521.             default:
  522.                 return (DefWindowProc (hWnd,wMsg,
  523.                                        wParam,lParam));
  524.             }
  525.         case IDC_ITALIC:
  526.             switch (HIWORD (lParam))
  527.             {
  528.             case BN_CLICKED:
  529.                 Checked = SendMessage (HItalic,BM_GETCHECK,
  530.                                         NULL,NULL);
  531.                 LF.lfItalic = Checked;
  532.                 if (HFont != NULL)
  533.                     DeleteObject (HFont);
  534.                 HFont = CreateFontIndirect (&LF);
  535.                 InvalidateRect (HWindow,&Rect,TRUE);
  536.                 UpdateWindow (HWindow);
  537.                 return (NULL);
  538.             default:
  539.                 return (DefWindowProc (hWnd,wMsg,wParam,lParam));
  540.             }
  541.         case IDC_SIZELIST:
  542.             switch (HIWORD(lParam))
  543.             {
  544.                 case LBN_SELCHANGE:
  545.                     Index = SendMessage (HSizeList,
  546.                                 LB_GETCURSEL,
  547.                                 0,
  548.                                 0L);
  549.                     FontSize = SizeList [Index];
  550.                     LF.lfHeight = -SizeList [Index];
  551.                     if (HFont != NULL)
  552.                         DeleteObject (HFont);
  553.                     HFont = CreateFontIndirect (&LF);
  554.                     InvalidateRect (HWindow,&Rect,TRUE);
  555.                     UpdateWindow (HWindow);
  556.                     return (NULL);
  557.                 default:
  558.                     return (DefWindowProc (hWnd, wMsg,
  559.                             wParam, lParam));
  560.             }
  561.         case IDC_UNDERLINE:
  562.             switch (HIWORD (lParam))
  563.             {
  564.                 case BN_CLICKED:
  565.                     Checked = SendMessage (HUnderline,
  566.                                 BM_GETCHECK,NULL,NULL);
  567.                     LF.lfUnderline = Checked;
  568.                     if (HFont != NULL)
  569.                         DeleteObject (HFont);
  570.                     HFont = CreateFontIndirect (&LF);
  571.                     InvalidateRect (HWindow,&Rect,TRUE);
  572.                     UpdateWindow (HWindow);
  573.                     return (NULL);
  574.                 default:
  575.                     return (DefWindowProc (hWnd,wMsg,
  576.                                 wParam,lParam));
  577.             }
  578.         default:
  579.             return (DefWindowProc (hWnd, wMsg, wParam, lParam));
  580.         }
  581.     case WM_DESTROY:
  582.         PostQuitMessage (0);
  583.         return (NULL);
  584.     case WM_PAINT:
  585.         HWinDC = BeginPaint (HWindow, &PaintStruct);
  586.         HFontOld = SelectObject (HWinDC,HFont);
  587.         TextOut
  588.             (HWinDC,
  589.             0,
  590.             0,
  591.             "Abcdefghijklmnopqrstuvwxyz",
  592.             26);
  593.         SelectObject (HWinDC,HFontOld);
  594.         EndPaint (HWindow,&PaintStruct);
  595.         return (NULL);
  596.     default:
  597.         return (DefWindowProc (hWnd, wMsg, wParam, lParam));
  598.     } /* end switch */
  599. } /* end MainWndProc */